package com.video.common.utils;

import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.messages.Item;

public class DelFile {

    public static void deleteFolder(MinioClient minioClient, String bucketName, String folderPath) {
        try {
            // 列出文件夹中的所有对象
            Iterable<Result<Item>> results = minioClient.listObjects(
                    ListObjectsArgs.builder()
                            .bucket(bucketName)
                            .prefix(folderPath)
                            .recursive(true)
                            .build());

            // 遍历并删除每个对象
            for (Result<Item> result : results) {
                Item item = result.get();
                minioClient.removeObject(
                        RemoveObjectArgs.builder()
                                .bucket(bucketName)
                                .object(item.objectName())
                                .build());
            }


            System.out.println("folderPath:"+folderPath);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Error deleting folder: " + e.getMessage());
        }
    }
}
